//
//  BN_TeacherMeansViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-26.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#define CELLFONT 14
#define SIGN_X_SPACE 5
#define SIGN_Y_SPACE 2.5
#import "BN_TeacherMeansViewController.h"

@interface BN_TeacherMeansViewController () {
    // code...
    NSArray *fileNameArr;
    NSArray *fileContentArr;
    NSString *signStr;
    
    UIButton *backVcBtn;
    MJRefreshHeaderView *header_;
}

@end

@implementation BN_TeacherMeansViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Data
    userInfo = [BN_UserHelper unArchiverAccountInfo];
    fileNameArr = @[@"毕业院校",@"学历",@"电话号码",@"教育格言"];
    fileContentArr = @[@"北京师范大学",@"大学本科",@"15511123752",@"世界上没有才能的人是没有的。问题在于教育者要去发现每一位学生的禀赋、兴趣、爱好和特长，为他们的表现和发展提供充分的条件和正确引导。 —— 苏霍姆林斯基"];
    
    // UITableView
    _teacherMeansTableView = [[UITableView alloc]init];
    _teacherMeansTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT);
    [self setExtraCellLineHidden:_teacherMeansTableView];
    if ([_teacherMeansTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_teacherMeansTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    _teacherMeansTableView.delegate = self;
    _teacherMeansTableView.dataSource = self;
    [self.view addSubview:_teacherMeansTableView];
    
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = _teacherMeansTableView;
    header.delegate = self;
    header_ = header;
    
    backVcBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backVcBtn.frame = CGRectMake(9, 20, 44, 44);
    [backVcBtn setImage:[UIImage imageNamed:@"tuichu"] forState:UIControlStateNormal];
    [backVcBtn addTarget:self action:@selector(backRootVc) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backVcBtn];
}

#pragma mark 
#pragma mark 加载网络
- (void)loadNetData {
    // code...
}

#pragma mark
#pragma mark 第一条cell
- (BN_LookTeacherInfoCell *)lookTeacherInfoCell {
    if (!_lookTeacherInfoCell) {
        _lookTeacherInfoCell = [[[NSBundle mainBundle]loadNibNamed:@"BN_LookTeacherInfoCell" owner:nil options:nil]objectAtIndex:0];
        _lookTeacherInfoCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return _lookTeacherInfoCell;
}

#pragma mark
#pragma mark UITableView 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 && indexPath.section == 0) {
        return 180;
    }else if (indexPath.section == 1 && indexPath.row ==3) {
        CGSize size = [self contentHeight];
        return size.height;
    }else {
        return 44;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    headerView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 20;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger rowCount = 0;
    switch (section) {
        case 0:
            rowCount = 1;
            break;
        case 1:
            rowCount = 4;
            break;
        default:
            break;
    }
    return rowCount;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 && indexPath.section == 0) {
        return self.lookTeacherInfoCell;
    }
    static NSString *identifier = @"BN_LookTeacherInfoBottomCell";
    BN_LookTeacherInfoBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"BN_LookTeacherInfoBottomCell" owner:nil options:nil]objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // 填写内容
    cell.fileNameLabel.text = fileNameArr[indexPath.row];
    cell.fileContentLabel.text = fileContentArr[indexPath.row];
    
    if (indexPath.section == 1 && indexPath.row ==3) {
        signStr = cell.fileContentLabel.text;
        CGSize size = [self contentHeight];
        cell.fileContentLabel.textAlignment = NSTextAlignmentLeft;
        cell.fileContentLabel.numberOfLines = 0;
        cell.fileContentLabel.frame = CGRectMake(90+SIGN_X_SPACE, CGRectGetMinY(cell.fileContentLabel.frame)+SIGN_Y_SPACE, 215, size.height);
        [cell.fileContentLabel sizeToFit];
    }
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    float y = _teacherMeansTableView.contentOffset.y;
    backVcBtn.frame = CGRectMake(9, 5+20-y, 44, 44);
}

#pragma mark
#pragma mark 刷新类
- (void)upLoadFinished:(MJRefreshBaseView *)refreshView {
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [refreshView endRefreshing];
}

- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    // 列表开始刷新，开始数据请求
    [self loadNetData];
    
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [refreshView endRefreshing];
}

- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if (header_ == refreshView) {
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:0.0];
    }else {
        [self performSelector:@selector(upLoadFinished:) withObject:refreshView afterDelay:0.0];
    }
}

- (void)refreshViewEndRefreshing:(MJRefreshBaseView *)refreshView
{
    // code...
}

- (void)refreshView:(MJRefreshBaseView *)refreshView stateChange:(MJRefreshState)state
{
    switch (state) {
        case MJRefreshStateNormal:
            break;
            
        case MJRefreshStatePulling:
            break;
            
        case MJRefreshStateRefreshing:
            break;
        default:
            break;
    }
}

#pragma mark 
#pragma mark Action 

- (void)backRootVc {
    [self releaseResource];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark
#pragma mark 工具方法
// 封装自动撑开高度的方法
- (CGSize)getTextSize:(CGSize)provisionsSize disFont:(UIFont *)font contentStr:(NSString *)contentStr contextDic:(NSDictionary *)contextDic {
    CGSize size;
    switch (MAIN_VERSION) {            
        case 6:
            size = [contentStr sizeWithFont:font constrainedToSize:provisionsSize lineBreakMode:NSLineBreakByWordWrapping];
            break;
        default:
            size =[contentStr boundingRectWithSize:provisionsSize options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:contextDic context:nil].size;
            break;
    }
    return size;
}

- (CGSize)contentHeight {
    CGSize contLabelsize = CGSizeMake(215,10000);
    UIFont *font = [UIFont systemFontOfSize:CELLFONT];
    NSDictionary * contLabeldic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
    CGSize contLabelActualsize = [self getTextSize:contLabelsize disFont:font contentStr:signStr contextDic:contLabeldic];
    return contLabelActualsize;
}

#pragma mark
#pragma mark 资源

- (void)releaseResource {
    _teacherMeansTableView.delegate = nil;
    _teacherMeansTableView.dataSource = nil;
    _teacherMeansTableView = nil;
    
    [header_ free];
    header_.delegate = nil;
    header_ = nil;
}

- (void)dealloc
{
    [self releaseResource];
    NSLog(@"老师资料释放...");
}

@end
